﻿using System;
using System.Drawing;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;

namespace DynamicSettings.Layouts.BramNuyts.DynamicSettings
{
    public partial class DynamicSettings : LayoutsPageBase
    {
        private SPWeb curWeb;
        private PropertyList propertyList;

        protected void Page_Load(object sender, EventArgs e)
        {
            curWeb = SPContext.Current.Web;
            CheckPrerequisites(curWeb);

            propertyList = new PropertyList(curWeb);

            ExistingProperties.DataSource = propertyList;
            ExistingProperties.AutoGenerateDeleteButton = true;
            ExistingProperties.RowDeleting += new GridViewDeleteEventHandler(this.RemoveGridProperty);
            UpdateView();
        }

        private void CheckPrerequisites(SPWeb curWeb)
        {
            if (curWeb.Features[new Guid("bc147c1a-1bd2-4790-94f4-2cb78064f6a7")] == null)
            {
                SPUtility.TransferToErrorPage("This page can only be used when the feature is activated.");
            }
            if (curWeb.GetProperty(Constants.DynamicSettingsKey) == null)
            {
                SPUtility.TransferToErrorPage(String.Format("This page can only be used when the {0} key is present.", Constants.DynamicSettingsKey));
            }
        }

        protected void AddProperty(object sender, EventArgs e)
        {
            try
            {
                propertyList.Add(new Property(txt_propertyKey.Text, txt_propertyValue.Text));
                propertyList.Update();
                UpdateView();
                AddPropertyStatus.ForeColor = Color.Green;
                AddPropertyStatus.Text = String.Format("Successfully added {0}", txt_propertyKey.Text);
            }
            catch (Exception ex)
            {
                AddPropertyStatus.ForeColor = Color.Red;
                AddPropertyStatus.Text = ex.Message;
            }
        }

        private void RemoveGridProperty(Object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                TableCell Key = ExistingProperties.Rows[e.RowIndex].Cells[1];
                propertyList.Remove(Key.Text);
                propertyList.Update();
                UpdateView();
            }
            catch (Exception ex)
            {
                RemovePropertyStatus.ForeColor = Color.Red;
                RemovePropertyStatus.Text = ex.Message;
            }
        }

        private void UpdateView()
        {
            AddPropertyStatus.Text = "";
            RemovePropertyStatus.Text = "";
            ExistingProperties.DataBind();
            if (ExistingProperties.Rows.Count == 0)
            {
                RemovePropertyStatus.Text = "There are currently no dynamic properties.";
            }
            else
            {
                RemovePropertyStatus.Text = "";
            }
        }
    }
}
